<?php
namespace Home\Controller;
use Think\Controller;


class NeedlistController extends Controller {

      //查询所有的需求信息
	function  allNeedlist(){
        $need=M("Needlist");
        $result=$need->select();
          if ($result) {
    		#
    		$this->ajaxReturn(array(
                "code"=>200,
                "message"=>"查询成功！",
                "data"=>$result
	    			),"JSON");
	    	}
	    	else{
	    		$this->ajaxReturn(array(
	                "code"=>500,
	                "message"=>"没有相应数据！"
	    			),"JSON");
	    	}

	}
 
    

  //按货品id查询所有的需求信息
  function  GoodsIdNeedlist(){

       $need=M("Needlist");
       $goodsid=$_POST["goodsid"];
       $result=$need->where("goods_id='$goodsid'")->select();
          if ($result) {
        $this->ajaxReturn(array(
                "code"=>200,
                "message"=>"查询成功！",
                "data"=>$result
            ),"JSON");
        }
        else{
          $this->ajaxReturn(array(
                  "code"=>500,
                  "message"=>"没有相应数据！"
            ),"JSON");
        }


  }

	//按地区或按物品类别查询

	function inquireNeedlist(){

        $address=$_POST["address"];
        $goodsclassify=$_POST["goodsclassify"];
        $need=M("Needlist");

        if ($address&$goodsclassify) {
              $data=$need
            ->field("t_needlist.needlist_goodsname,t_needlist.needlist_number,t_needlist.needlist_time,t_needlist.needlist_desc") 
            ->join("t_address on t_needlist.address_id=t_address.address_id")
            ->join("t_goodsclassify on t_needlist.goodsclassify_id=t_goodsclassify.goodsclassify_id")
            ->where("t_needlist.goodsclassify_id='$goodsclassify'and t_needlist.address_id='$address'")
            ->select();

            if ($data) {
                #
                $this->ajaxReturn(array(
                    "code"=>200,
                    "message"=>"查询成功！",
                    "data"=>$data
                    ),"JSON");
            }else{
                $this->ajaxReturn(array(
                    "code"=>500,
                    "message"=>"没有需求！"
                    ),"JSON");
            }
        }elseif ($address&!$goodsclassify) {
            $data=$need
            ->field("t_needlist.needlist_goodsname,t_needlist.needlist_number,t_needlist.needlist_time,t_needlist.needlist_desc") 
            ->join("t_address on t_needlist.address_id=t_address.address_id")
            ->where("t_needlist.address_id='$address'")
            ->select();

            if ($data) {
                #
                $this->ajaxReturn(array(
                    "code"=>200,
                    "message"=>"查询成功！",
                    "data"=>$data
                    ),"JSON");
            }else{
                $this->ajaxReturn(array(
                    "code"=>500,
                    "message"=>"没有需求！"
                    ),"JSON");
            }
        }elseif (!$address&$goodsclassify) {
             $data=$need
            ->field("t_needlist.needlist_goodsname,t_needlist.needlist_number,t_needlist.needlist_time,t_needlist.needlist_desc") 
            ->join("t_goodsclassify on t_needlist.goodsclassify_id=t_goodsclassify.goodsclassify_id")
            ->where("t_needlist.goodsclassify_id='$goodsclassify'")
            ->select();

            if ($data) {
                #
                $this->ajaxReturn(array(
                    "code"=>200,
                    "message"=>"查询成功！",
                    "data"=>$data
                    ),"JSON");
            }else{
                $this->ajaxReturn(array(
                    "code"=>500,
                    "message"=>"没有需求！"
                    ),"JSON");
            }
        }
	}
  


    //按需求表id 查询

    function inqureGoodsInfoById (){
        $listID=$_POST["listid"];
        $type=$_POST["type"];
        $need=M("Needlist");

        if ($type==1) {
            $data=$need
            ->field("t_address.address_name,t_goodsclassify.goodsclassify_name") 
            ->join("t_address on t_needlist.address_id=t_address.address_id")
            ->join("t_goodsclassify on t_needlist.goodsclassify_id=t_goodsclassify.goodsclassify_id")
            ->where("t_needlist.needlist_id='$listID'")
            ->select();

            if ($data) {
                #
                $this->ajaxReturn(array(
                    "code"=>200,
                    "message"=>"查询成功！",
                    "data"=>$data[0]
                    ),"JSON");
            }else{
                $this->ajaxReturn(array(
                    "code"=>500,
                    "message"=>"没有需求！"
                    ),"JSON");
            }
        }
    }


    //发布采购
    public function addneed(){

      $addneeds=M("Needlist");

      $goodsname=$_POST["goodsname"];
      $number=$_POST["number"];
      $time=$_POST["time"];
      $surplustime=$_POST["plustime"];
      $linkman=$_POST["linkman"];
      $tel=$_POST["tel"];
      $desc=$_POST["desc"];
      $timeinterrval=$_POST["timeinterrval"];
      $userid=$_POST["userid"];
      $addid=$_POST["addid"];
      $goodclassid=$_POST["goodclassid"];
      $goodid=$_POST["goodid"];

      $newdata["needlist_goodsname"]=$goodsname;
      $newdata["needlist_number"]=$number;
      $newdata["needlist_time"]=$time;
      $newdata["needlist_surplustime"]=$surplustime;
      $newdata["needlist_linkman"]=$linkman;
      $newdata["needlist_linkmanphone"]=$tel;
      $newdata["needlist_desc"]=$desc;
      $newdata["needlist_timeinterrval"]=$timeinterrval;
      $newdata["user_id"]=$userid;
      $newdata["address_id"]=$addid;
      $newdata["goodsclassify_id"]=$goodclassid;
      $newdata["goods_id"]=$goodid;

      $result=$addneeds->add($newdata);       
       if ($result) {
          $this->ajaxReturn(array("code"=>200,"message"=>"发布成功","data"=>$result),"JSON");
       }else{
            $this->ajaxReturn(array("code"=>500,"message"=>"发布失败","data"=>NULL),"JSON");
       }  

    }

    //卖家首页中的今日推荐中的信息
    function Recommed(){

      $need=M("Needlist");
      $result=$need
      ->field("t_needlist.user_id,t_privilegelist.privilege_id,t_needlist.needlist_goodsname,t_needlist.needlist_number,
        t_needlist.needlist_time,t_needlist.needlist_desc,t_needlist.address_id,t_needlist.goodsclassify_id,t_needlist.needlist_id")
      ->join("t_privilegelist on t_needlist.user_id=t_privilegelist.user_id")
      ->select();
         if ($result) {
        $this->ajaxReturn(array(
                "code"=>200,
                "message"=>"查询成功！",
                "data"=>$result
            ),"JSON");
        }
        else{
          $this->ajaxReturn(array(
                  "code"=>500,
                  "message"=>"没有相应数据！"
            ),"JSON");
        }
        var_dump($result);













    }
    










}